var api = require('../../utils/api.js')
var app = getApp()
Page({
    data: {
        code:'',
        show:0,
        openid:'',
        usrl:'',
        xieyi:0,
        mobile:'',
        captcha:'',
        codetext:'获取验证码',
        info:''
    },

   
    setxieyi1(){
        this.setData({
            xieyi:1
        })
    },
    setxieyi0(){
        this.setData({
            xieyi:0
        })
    },
    onLoad() {
        var usrl=api.HOST;
        var that=this;
        api.post({
            url: 'index/getcompinfo',
            data: {},
            success: data => {
                that.setData({
                    info:data.data
                })
            }
        })


        this.setData({
            usrl:usrl
        })
    },
    onShow(){
        //调用登录接口
        var that=this;

    },
 
    getcaptcha(ret){
        this.setData({
            captcha:ret.detail.value
        })
    },
    goindex(){
        wx.switchTab({
          url: '/pages/index/index',
        })
    },
    getPhoneNumber: function(e){
        console.log(e.detail);
        var code=e.detail.code;
        api.post({
            url: 'wxapp/getmobile',
            data: {'code':code},
            success: data => {
                console.log(data);

                wx.showToast({
                    title: data.msg,
                    icon:'none'
                  })

                if(data.code==1){
                    wx.setStorageSync('userinfo', data.data.userinfo);
                    wx.setStorageSync('token', data.data.userinfo.token);

                    wx.switchTab({
                      url: '/pages/my/my',
                    })
                }
            }
        })
    },
    seticon0(ret){
        this.setData({
                show:0
        })
    },
    seticon1(){
        this.setData({
            show:1
        })
    },
    getcodebynumber(){
        var mobile=this.data.mobile;
        var codetext=this.data.codetext;
        var that=this;
        console.log(mobile,codetext)
        if(codetext!='获取验证码'){
            return false;
        }
        if(!mobile){
            wx.showToast({
              title: '请输入手机号',
              icon:'none'
            })
            return false;
        }

        api.post({
            url: 'sms/send',
            data: {'mobile':mobile},
            success: data => {
                console.log(data)
                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })
                if(data.code==1){
                    that.setovertime();
                }
            }
        })
    },

    setovertime(){
        var int=60;
        var that=this;
        var index=setInterval(function(){
            int--;
            if(int<1){
                clearInterval(index);
                that.setData({
                    codetext:'获取验证码'
                })
            }else{
                that.setData({
                    codetext:int+'s后获取'
                })
            }

        },1000)
    },


    getnumber(ret){
        this.setData({
            mobile:ret.detail.value
        })
    },

    loginbysms(){
        var mobile=this.data.mobile;
        var captcha=this.data.captcha;
        var xieyi=this.data.xieyi;

        if(xieyi!=1){

            wx.showToast({
              title: '请阅读并同意用户协议',
              icon:'none'
            })
            return false;
        }


        var that=this;
        api.post({
            url: 'user/mobilelogin',
            data: {'mobile':mobile,'captcha':captcha},
            success: data => {
                console.log(data);

                wx.showToast({
                  title: data.msg,
                  icon:'none'
                })

                if(data.code==1){
                    wx.setStorageSync('userinfo', data.data.userinfo);
                    wx.setStorageSync('token', data.data.userinfo.token);

                    wx.switchTab({
                      url: '/pages/my/my',
                    })
                }
            }
        })
    },
    seexieyi(){
        wx.navigateTo({
          url: '/pages/login/xieyi',
        })
    }
})
